/////////////////////////////////////////////
// AcDbEntityReactor reactors.

#include "StdAfx.h"
#include "StdArx.h"

CMAWinReact::CMAWinReact(AcDbObjectId objId)
{
	m_ObjId = objId;
}


void CMAWinReact::modified(const AcDbObject* dbObj)
{
	CMARectWindow *pRW1, *pRW2;
	AcDbEntity *pEnt;
	AcGePoint3d newLwrLeftPt;
	AcGeVector3d transVec;
	AcGeMatrix3d mat;
	Acad::ErrorStatus es;
	char strError[80];

    pRW1 = CMARectWindow::cast(dbObj);
    if(!pRW1)
	{
        const char *cstr = dbObj->isA()->name();
        acutPrintf("This is a %s.\n", cstr);
        acutPrintf("I only work with CMARectWindow custom entities. Sorry.\n");
        return;
    }
	
	newLwrLeftPt = pRW1->getWindowStartPoint();
	transVec = newLwrLeftPt - m_llPt;
	m_llPt = newLwrLeftPt;
	mat.setToTranslation(transVec);

	es = acdbOpenObject(pEnt, m_ObjId, AcDb::kForWrite);
    if(es != Acad::eOk)
	{
		if(es == Acad::eWasOpenForWrite) // 83
		{
			strcpy(strError, " (open for write)");
		}
		else if(es == Acad::eWasNotifying) // 85
		{
			strcpy(strError, " (open for notify)");
		}
		else
		{
			strcpy(strError, "");
		}
        acutPrintf("\nacdbOpenObject in WinReact failed with error %d%s.\n",
					es, strError);
        return;
    }

    pRW2 = CMARectWindow::cast(pEnt);
    if (!pRW2) {
        const char *pCstr = pEnt->isA()->name();
        acutPrintf("This reactor is attached to a %s.\n", pCstr);
        acutPrintf("It should only be attached to RectWindow custom Entities. Sorry.\n");
        pEnt->close();
        return;
    }

	pRW2->transformBy(mat);

	pRW2->close();
}


void CMAWinReact::setLwrLeftPoint(AcGePoint3d llPt)
{
	m_llPt = llPt;
}
